﻿Imports System.Data.Entity.Validation
Imports System
Imports System.Security.Claims
Imports System.Web.UI
Imports System.Data.OleDb
Imports System.IO
Imports System.Collections.Generic

Public Class sms_money_data
    Inherits System.Web.UI.Page

    Dim idinfo As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub



    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_moneytype.SelectedIndexChanged
        idinfo = ddl_moneytype.SelectedValue

    End Sub






    Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
        Dim id As Integer
        id = Convert.ToInt32(GridView1.DataKeys(e.NewEditIndex).Value)



        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
        Dim SqlString As String = "SELECT * FROM sms_moneydata where ID =  @ID"

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)


        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@ID", id)

        oleDbConn.Open()

        Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim ds As DataSet = New DataSet()
        sda.Fill(ds)


        Dim dr As DataRow = ds.Tables(0).Rows(0)
        If (dr("moneytype_fkid").ToString() = "") Then

        Else

            ddl_moneytype.SelectedValue = dr("moneytype_fkid").ToString()
        End If
        If (dr("moneysubtype_fkid").ToString() = "") Then
        Else
            ddl_submoney.SelectedValue = dr("moneysubtype_fkid").ToString()
        End If

        hdnId.Value = id.ToString()

        company_name.Text = dr("Company_Name").ToString()
        company_url.Text = dr("companyurl").ToString()
        If (dr("Stars").ToString() = "1") Then

            RadioButton1.Checked = True
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False

        ElseIf (dr("Stars").ToString() = "2") Then
            RadioButton2.Checked = True
            RadioButton1.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False
        ElseIf (dr("Stars").ToString() = "3") Then
            RadioButton3.Checked = True
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False
        ElseIf (dr("Stars").ToString() = "4") Then
            RadioButton4.Checked = True
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton5.Checked = False
        ElseIf (dr("Stars").ToString() = "5") Then
            RadioButton5.Checked = True
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
        Else
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            RadioButton4.Checked = False
            RadioButton5.Checked = False
        End If

        txt_desc.Text = dr("Description").ToString()
        location.Text = dr("Location").ToString()
        contact_details.Text = dr("Contact_Details").ToString()
        lbl_pic.Text = dr("Picture").ToString()
        txtfl1.Text = dr("Picture").ToString()
        Disable()
        Button1.Text = "Edit"



    End Sub
    Private Sub Disable()
        ddl_moneytype.Enabled = False
        ddl_submoney.Enabled = False
        company_name.Enabled = False
        company_url.Enabled = False
        RadioButton1.Enabled = False
        RadioButton2.Enabled = False
        RadioButton3.Enabled = False
        RadioButton4.Enabled = False
        RadioButton5.Enabled = False
        divfl1.Visible = False
        txtfl1.Visible = False
        contact_details.Enabled = False
        txt_desc.Enabled = False
        location.Enabled = False
        lbl_pic.Visible = True
    End Sub

    Private Sub Enable()
        ddl_moneytype.Enabled = True
        ddl_submoney.Enabled = True
        divfl1.Visible = True
        txtfl1.Visible = True
        company_name.Enabled = True
        company_url.Enabled = True
        RadioButton1.Enabled = True
        RadioButton2.Enabled = True
        RadioButton3.Enabled = True
        RadioButton4.Enabled = True
        RadioButton5.Enabled = True
        contact_details.Enabled = True
        txt_desc.Enabled = True
        location.Enabled = True
        lbl_pic.Visible = False

    End Sub

    Protected Sub btncancel_Click(sender As Object, e As EventArgs)
        Button1.Text = "Edit"
        Disable()


    End Sub

    Public Sub GetDetails()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString004").ConnectionString)
        Dim SqlString As String

        SqlString = "SELECT * FROM [sms_moneydata] order by ID Desc"

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim ds As DataSet = New DataSet()
        sda.Fill(ds)

        GridView1.DataSource = ds.Tables(0)
        GridView1.DataBind()





    End Sub

    Protected Sub Button1_Click1(sender As Object, e As EventArgs)
        Try
            Dim fileName As String

            If Button1.Text = "Edit" Then
                Enable()
                Button1.Text = "Save"
                Button1.ValidationGroup = "add"
            ElseIf Button1.Text = "Save" Then

                If fl1.HasFile Then
                    fileName = Path.GetFileName(fl1.PostedFile.FileName)
                    fl1.PostedFile.SaveAs(Server.MapPath("~/Upload/") + fileName)
                Else
                    fileName = lbl_pic.Text
                End If

                Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString004").ConnectionString)
                Dim SqlString As String

                SqlString = "Update sms_moneydata set moneytype_fkid = @f1 ,moneysubtype_fkid=@f2, Company_Name=@f3, Location=@f4,Stars=@f5,Description=@f6,Contact_Details=@f7,Picture=@f8,companyurl=@f9 where ID = " + hdnId.Value

                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", ddl_moneytype.SelectedValue)
                cmd.Parameters.AddWithValue("@f2", ddl_submoney.SelectedValue)
                cmd.Parameters.AddWithValue("@f3", company_name.Text)
                cmd.Parameters.AddWithValue("@f4", location.Text)

                If RadioButton1.Checked = True Then
                    cmd.Parameters.AddWithValue("@f5", 1)
                ElseIf RadioButton2.Checked = True Then
                    cmd.Parameters.AddWithValue("@f5", 2)
                ElseIf RadioButton3.Checked = True Then
                    cmd.Parameters.AddWithValue("@f5", 3)
                ElseIf RadioButton4.Checked = True Then
                    cmd.Parameters.AddWithValue("@f5", 4)
                ElseIf RadioButton5.Checked = True Then
                    cmd.Parameters.AddWithValue("@f5", 5)
                Else
                    lblError.Text = "Please select star"
                    Return
                End If

                cmd.Parameters.AddWithValue("@f6", txt_desc.Text)
                cmd.Parameters.AddWithValue("@f7", contact_details.Text)
                cmd.Parameters.AddWithValue("@f8", fileName)
                cmd.Parameters.AddWithValue("@f9", company_url.Text)

                oleDbConn.Open()
                cmd.ExecuteNonQuery()
                GridView1.DataSourceID = "SqlDataSource005"
                GridView1.DataBind()
                Disable()
                Button1.Text = "Edit"
                GridView1.EditIndex = -1
                Clear()
            End If

        Catch ex As DbEntityValidationException

        End Try

    End Sub

    Private Sub Clear()

        lbl_pic.Text = ""
        company_name.Text = ""
        company_url.Text = ""
        RadioButton1.Checked = False
        RadioButton2.Checked = False
        RadioButton3.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
        contact_details.Text = ""
        txt_desc.Text = ""
        location.Text = ""


    End Sub


    Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)

        GridView1.PageIndex = e.NewPageIndex
    End Sub
    Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
        Try
            Dim id As Integer
            id = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "delete  FROM sms_moneydata where ID=@ID"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@ID", id)


            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            ' bindgrid()


        Catch ex As DbEntityValidationException

        End Try
    End Sub
End Class